#ifndef __M_UTIL_H__
#define __M_UTIL_H__
#include <iostream>
#include <sstream>
#include <string>
#include <jsoncpp/json/json.h>

namespace im_sys{
	class JsonUtil{
		public:
			static bool Serialize(Json::Value &value, std::string *jsonstr){
				std::stringstream ss;
				Json::StreamWriterBuilder swb;
				Json::StreamWriter* sw =swb.newStreamWriter();
				int ret=sw->write(value,&ss);
				if(ret!=0){
					std::cout<<"json write failed!\n";
					delete sw;
					return false;
				}
				*jsonstr=ss.str();
				delete sw;
				return true;
			}
			static bool UnSerialize(const std::string &jsonstr,Json::Value *value){
				Json::CharReaderBuilder crb;
				Json::CharReader* cr = crb.newCharReader();
				std::string err;
				bool ret=cr->parse(jsonstr.c_str(),jsonstr.c_str()+jsonstr.size(),value,&err);
				if(ret==false){
					delete cr;
					std::cout<<"json parse failed! "<<err<<std::endl;
					return false;
				}
				delete cr;
				return true;
			}
	};
}

#endif
